﻿//for examples of use in an actual game see Nat Geo fight science loader

import mx.utils.Delegate;
import arkadium.AppConfigFile;
import arkadium.ArkMc;
import arkadium.EventManager;
import arkadium.XMLFile;
import FightScienceLoader_Code.DataManager
import FightScienceLoader_Code.Main;

class FightScienceLoader_Code.LoginArea extends ArkMc
{
	var _cDataManager;
	var _smcUser:MovieClip
	var _smcPass:MovieClip
	var _smcMessageText:MovieClip
	var _sdtErrors:TextField
	var _sbtLogout:Button
	var _sbtLogin:Button
	var _sbtRegister:Button
	var _sbtCancel:Button
	var _sEvent:String
	
	var _sLoginUrl:String
	
	var loginXML:XMLFile
	
	public function LoginArea(){
		_cDataManager = DataManager.getInstance();
	}

	public function onInitialize(){
		super.onInitialize();
	}
	
	public function onReady(){
		_smcUser._sitUser.restrict = "A-Z a-z 0-9";
		_smcPass._sitPass.restrict = "A-Z a-z 0-9";
		_smcUser._sitUser.text = "Username";
		_smcPass._sitPass.text = "Password";
		
		_sLoginUrl  = Main.getInstance()._cAppConfigFile.getData("loginUrl");
		if(_sLoginUrl==undefined)_sLoginUrl = "http://72.3.138.97/ngservice/auth.aspx"//"http://testing.arkadium.com/arkadium_Leaderboard/auth.aspx"
		loginXML = new XMLFile();
		
		this.setTriggerEventByName("guestFrame",Delegate.create(this,onGuestFrame));
		this.setTriggerEventByName("loginFrame",Delegate.create(this,onLoginFrame));
		this.setTriggerEventByName("userFrame",Delegate.create(this,onUserFrame));
		this.setTriggerEventByName("registerFrame",Delegate.create(this,onRegisterFrame));
		
		this.setTriggerEventByName("endGuestFrame",Delegate.create(this,doNextEvent));
		this.setTriggerEventByName("endLoginFrame",Delegate.create(this,doNextEvent));
		this.setTriggerEventByName("endUserFrame",Delegate.create(this,doNextEvent));
		this.setTriggerEventByName("endRegisterFrame",Delegate.create(this,doNextEvent));

		if (checkLoggedIn()){
			_sEvent = "startUser";
		}
		else {
			_sEvent = "startGuest"
		}
		
		doNextEvent();
	}
	
	public function checkLoggedIn(){
		return(_cDataManager._bLoggedIn)
	}
	
	public function doNextEvent(){
		this[_sEvent+"Situation"]();
	}
	
	public function startUserSituation(){
		this.playAnimation("startUserFrame");
		_smcMessageText._sdtMessageText.text = "Welcome, "+_cDataManager._cMyCharacter._sUserName;
	}
	
	public function onUserFrame(){
		this.stopAnimation();
		_sbtLogout.onRelease = Delegate.create( this, logout );
		_sbtLogout.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
	}
	
	public function logout(){
		_cDataManager._sUserName = "";
		_cDataManager._sUserGUID = "";
		_cDataManager._bLoggedIn =  false;
		_cDataManager.createNewCharacter();
		this.playAnimation("userFrame");
		_sEvent = "startGuest";
	}
	
	public function startGuestSituation(){
		_cDataManager._bLoggedIn =  false;
		_sEvent = "startGuest"
		this.playAnimation("startGuestFrame");
	}
	
	public function onGuestFrame(){
		this.stopAnimation();
		_sbtLogin.onRelease = Delegate.create( this, goBackAndLogin );
		_sbtLogin.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		_sbtRegister.onRelease = Delegate.create( this, goBackAndRegister );
		_sbtRegister.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
	}
	
	public function goBackAndLogin(){
		_sbtLogin.onRelease = function(){}
		_sbtRegister.onRelease = function(){}
		_sEvent = "startLogin";
		this.playAnimation("guestFrame");
	}
	
	public function goBackAndRegister(){
		_sbtLogin.onRelease = function(){}
		_sbtRegister.onRelease = function(){}
		_sEvent = "startRegister";
		this.playAnimation("guestFrame");
	}
	
	public function startRegisterSituation(){
		this.playAnimation("startRegisterFrame");
		_sEvent = "startRegister"
		_sdtErrors.text = "";
		_smcUser._sitUser.text = "Username";
		_smcPass._sitPass.text = "Password";
	}
	
	public function onRegisterFrame(){
		this.stopAnimation();
		_sbtRegister.onRelease = Delegate.create( this, register );
		_sbtRegister.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		_sbtCancel.onRelease = Delegate.create( this, cancel );
		_sbtCancel.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
	}
	
	public function cancel(){
		_sbtLogin.onRelease = function(){}
		_sbtRegister.onRelease = function(){}
		_sbtCancel.onRelease = function(){}
		this.playAnimation();
		_sEvent = "startGuest";
	}
	
	public function startLoginSituation(){
		this.playAnimation("startLoginFrame");
		_sdtErrors.text = "";
		_smcUser._sitUser.text = "Username";
		_smcPass._sitPass.text = "Password";
		
	}
	
	public function onLoginFrame(){
		this.stopAnimation();
		_sbtLogin.onRelease = Delegate.create( this, login );
		_sbtLogin.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
		_sbtCancel.onRelease = Delegate.create( this, cancel );
		_sbtCancel.onRollOver = function(){ Main.getInstance()._cSoundManager.rolloverSoundStart();   }
	}
	public function onLogin(){
		_sbtLogin.onRelease = function(){}
		_sbtCancel.onRelease = function(){}
		_sEvent = "startUser";
		_parent.doOnLogin();
		this.playAnimation();
	}
	
	public function login(){
		var sUnencryptedParams = 'userName='+_smcUser._sitUser.text+'&password='+_smcPass._sitPass.text+'&isNewUser='+0;
		var sParams = Main.getInstance()._cEncryptionManager.encryptParams(sUnencryptedParams) 
		loginXML.loadFile( this, checkLogin, _sLoginUrl+ "?params="+sParams );
	}
	
	public function register(){
		var sUnencryptedParams = 'userName='+_smcUser._sitUser.text+'&password='+_smcPass._sitPass.text+'&isNewUser='+1;
		var sParams = Main.getInstance()._cEncryptionManager.encryptParams(sUnencryptedParams) 
		loginXML.loadFile( this, checkRegister, _sLoginUrl+ "?params="+sParams );
	}

	public function checkLogin(bSuccess){
		if(bSuccess){
			var sErrorCode = loginXML.firstChild.attributes.errorcode;
			if(sErrorCode == "0"){
				_cDataManager._sUserName = _smcUser._sitUser.text;
				_cDataManager._sUserGUID = loginXML.firstChild.attributes.userGuid;
				_cDataManager._bLoggedIn =  true;
				_cDataManager.loadUser();
				onLogin();
			}
			else if (sErrorCode == "1"){
				_sdtErrors.text = "Username or password is not correct";
			}
			else if (sErrorCode == "2"){
				_sdtErrors.text = "Please fill in the Username and Password";
			}
			else{
				_sdtErrors.text = "There has been an error";
			}
		}
		else _sdtErrors.text = "Connection Error";
	}
	
	public function checkRegister(bSuccess:Boolean){
		if(bSuccess){
			var sErrorCode = loginXML.firstChild.attributes.errorcode;
			if(sErrorCode == "0"){
				_cDataManager._sUserName = _smcUser._sitUser.text;
				_cDataManager._sUserGUID = loginXML.firstChild.attributes.userGuid;
				_cDataManager._bLoggedIn =  true;
				_cDataManager.registerNewUser();
				onLogin();
			}
			else if (sErrorCode == "1"){
				_sdtErrors.text = "This username is taken";
			}
			else if (sErrorCode == "2"){
				_sdtErrors.text = "Please fill in the Username and Password";
			}
			else {
				_sdtErrors.text = "There has been an error";
			}
		}
		else _sdtErrors.text = "Connection Error";
	}
}